package org.momo.rpc;

import lombok.extern.slf4j.Slf4j;
import org.momo.rpc.api.OrderService;
import org.momo.rpc.api.UserService;
import org.momo.rpc.api.model.Order;
import org.momo.rpc.api.model.User;
import org.momo.rpc.bootstrap.ConsumerBootstrap;
import org.momo.rpc.proxy.ServiceProxyFactory;
import org.momo.rpc.bootstrap.model.ReferenceMetaInfo;


@Slf4j
public class ConsumerExample {

    public static void main(String[] args) throws Exception {
        ConsumerBootstrap.init();

//        ExecutorService rpcExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*2);
//        int nthread = 100000;
//        CountDownLatch countDownLatch = new CountDownLatch(nthread);
//        Runnable runnable = () -> {
//            try {
//                int calculate = userService.calculate(1);
//                // log.info("result: {}", calculate);
//            } catch (Exception e) {
//                log.error("{}", e.getMessage());
//            }
//            countDownLatch.countDown();
//        };
//
//        long start = System.currentTimeMillis();
//        for(int i = 0; i < nthread; ++i) {
//            rpcExecutor.execute(runnable);
//        }
//        countDownLatch.await();
//        long end = System.currentTimeMillis();
//        log.info("time: {}", end - start);

//        rpcExecutor.shutdown();

        ReferenceMetaInfo userInfo = new ReferenceMetaInfo(UserService.class, "user", "v1.0", null);
        UserService userService = ServiceProxyFactory.getProxy(userInfo);

        ReferenceMetaInfo orderInfo = new ReferenceMetaInfo(OrderService.class, "order", "v1.0", null);
        OrderService orderService = ServiceProxyFactory.getProxy(orderInfo);

        for(int i = 0; i < 1; ++i) {
            User user = userService.queryUserById((long)i);
            Order order = orderService.queryOrderById((long)i);
            log.info("query result: {} {}", user, order);
        }

        System.exit(0);
    }

}
